<?php
/**
 * 账号
 *
 * @author kaikaic
 * @date 2018-04-09
 *
 */

namespace app\api\service;

use app\admin\model\CategoryModel;
use app\admin\service\AdvService;
use app\admin\service\TagsService;
use service\ConfigService;
use think\Db;

class   MainService{
    /**
     * 根据广告位ID加载广告内容
     * @param $spotID,广告位ID
     * @return array
     */
    public static  function loadAD($spotID) {

        $adv =  Db::name('SystemAdv')->where('spot_id', $spotID)->select();

        $adList = array();
        for ($i=0; $i<count($adv); $i++) {
            $adList[] = array('image'=>$adv[$i]['image'], 'link'=>$adv[$i]['link'],'name'=>$adv[$i]['name'],'url'=>$adv[$i]['link']);
        }

        return array('type'=>'single', 'ad'=>$adList);
    }
    //获取订单列表
    public static function getOrderList($accountid,$order_status,$page,$size,$type){

        if($type==1){
            if($order_status==3){
                $orderList  = Db::name('psy_order')->where('account_id', $accountid)->where('order_status',1)->where('pay_status',2)->order('create_time desc')->limit($page*$size,$size)->select();
            }else if($order_status==1){
                $orderList  = Db::name('psy_order')->where('account_id', $accountid)->where('order_status',$order_status)->where('pay_status',1)->order('create_time desc')->limit($page*$size,$size)->select();
            }else if($order_status){
                $orderList  = Db::name('psy_order')->where('account_id', $accountid)->where('order_status',$order_status)->order('create_time desc')->limit($page*$size,$size)->select();
            }else{
                $orderList  = Db::name('psy_order')->where('account_id', $accountid)->order('create_time desc')->limit($page*$size,$size)->select();
            }
        }else{
            $orderList  = Db::name('psy_order')->where('consultant_id', $accountid)->where('order_status',$order_status)->order('order_time desc')->limit($page*$size,$size)->select();
        }
        foreach ($orderList as $key=>&$item){
            $params['id'] = $item['consultant_id'];
            $consult =  $item['consult']  =  ConsultantService::getInstance()->single($params);
            $tagsList = TagsService::getInstance()->getTagsArrayByAbbr("psy_qualification");
            if ($consult['qualification']) {
                $item['consult']['qualification'] = $tagsList[$consult['qualification']];
            }else {
                $item['consult']['qualification'] = "";
            }
            $item['type_txt'] = Db::name('system_category')->where('id',$item['type'])->value('name');
        }
        return $orderList;
    }

    //获取文章
    public static function getArticleList($param,$limit){
        $artInfo = Db::name("articles")
            ->field("*,FROM_UNIXTIME(create_time, '%Y-%m-%d ') as create_time")
            ->where($param)
            ->whereNotIn("id","158,159,160")
            ->limit($limit)->select();

        return $artInfo;
    }
    //获取测试介绍
    public static function getTestList($param,$limit){
        $artInfo = Db::name("psy_test")->where($param)->limit($limit)->select();
        foreach ($artInfo as $key=>&$item){
            $item['create_time'] = date('Y-m-d',$item['create_time']);
        }
        return $artInfo;
    }
    //获取课程介绍
    public static function getClassList($param,$limit){
        $artInfo = Db::name("psy_class")->where($param)->limit($limit)->select();
        foreach ($artInfo as $key=>&$item){
            $item['create_time'] = date('Y-m-d',$item['create_time']);
        }
        return $artInfo;
    }
    public static function getClassInfo($id){
        $Info = Db::name("psy_class")->find($id);
        return $Info;
    }
    public static function getTestInfo($id){
        $Info = Db::name("psy_test")->find($id);
        return $Info;
    }

    //获取文章
    public static function getArticle($art_id){
        $artInfo = Db::name("articles")->find($art_id);
        return $artInfo;
    }
    public static function getArticleType($arttype){
        $artList = Db::name("articles")->where('category_id',$arttype)->where('status',2)->select();
        return $artList;
    }

    public static function getOrderInfoByPaySn($paysn){
        $res = Db::name("psy_order")->where('pay_sn',$paysn)->find();

        return $res;
    }
    public static function getOrderInfoByOrdersn($ordersn){
        $res = Db::name("psy_order")->where('order_sn',$ordersn)->find();

        $statusArr = ConfigService::cfgOrderStatus();
        $params['id'] = $res['consultant_id'];
        $res['consult'] = $consult =  $res['consult']  =  ConsultantService::getInstance()->single($params);
        $tagsList = TagsService::getInstance()->getTagsArrayByAbbr("psy_qualification");
        if ($consult['qualification']) {
            $res['consult']['qualification'] = $tagsList[$consult['qualification']];
        }else {
            $res['consult']['qualification'] = "";
        }

        $res['type_txt'] = Db::name('system_category')->where('id',$res['type'])->value('name');
        $res['order_status_txt'] = $statusArr[ $res['order_status']];
        $res['create_time'] = date('Y-m-d H:i:s',$res['create_time']);
        $res['update_time'] = date('Y-m-d H:i:s',$res['update_time']);
        $res['cancel_time'] = date('Y-m-d H:i:s',$res['cancel_time']);
        return $res;
    }
    public static function cancelOrder($ordersn,$data){
        $res = Db::name("psy_order")->where('order_sn',$ordersn)->update($data);
        return $res;
    }
    public static function getMemberList($accountid){
        $memberList  = Db::name('account_member')->where('account_id', $accountid)->select();
        return $memberList;
    }
    public static function findMember($id){
        $memberInfo  = Db::name('account_member')->find($id);
        return $memberInfo;
    }
    public static function saveMemberInfo($data){
        if(isset($data['id'])){
            $id = $data['id'];
            unset($data['id']);
            $memberInfo  = Db::name('account_member')->where('id',$id)->update($data);
            if($memberInfo>=0){
                return true;
            }else{
                return false;
            }
        }else{
            if(Db::name('account_member')->insert($data)){
                return true;
            }else{
                return false;
            }
        }
    }
}
